﻿namespace Blaze.Editor.UI
{
    using UnityEngine.Rendering;

    /// <summary>
    /// 自定义混合模式，提供<see cref="BlendMode"/>友好的名称显示。
    /// </summary>
    public class CustomBlendMode
    {
        public static CustomBlendMode Additive = new CustomBlendMode(BlendOp.Add, BlendMode.SrcAlpha, BlendMode.One, "叠加");

        /// <summary>
        /// 获取所有的自定义混合模式。
        /// </summary>
        public static readonly CustomBlendMode[] All;

        public static CustomBlendMode Darken = new CustomBlendMode(BlendOp.Min, BlendMode.SrcAlpha, BlendMode.One, "变暗");
        public static CustomBlendMode Lighten = new CustomBlendMode(BlendOp.Max, BlendMode.SrcAlpha, BlendMode.One, "变亮");
        public static CustomBlendMode Multiply = new CustomBlendMode(BlendOp.Add, BlendMode.DstColor, BlendMode.Zero, "正片叠底");
        public static CustomBlendMode Normal = new CustomBlendMode(BlendOp.Add, BlendMode.SrcAlpha, BlendMode.OneMinusSrcAlpha, "正常");
        public static CustomBlendMode Undefined = new CustomBlendMode(BlendOp.Add, BlendMode.Zero, BlendMode.Zero, "未定义");

        public BlendMode Dest { get; private set; }
        public string FriendlyName { get; private set; }
        public BlendOp Op { get; private set; }
        public BlendMode Src { get; private set; }

        static CustomBlendMode()
        {
            All = new[]
            {
                Normal,
                Additive,
                Darken,
                Lighten,
                Multiply,
                Undefined,
            };
        }

        public CustomBlendMode(BlendOp op, BlendMode src, BlendMode dest, string friendlyName)
        {
            Src = src;
            Dest = dest;
            Op = op;
            FriendlyName = friendlyName;
        }

        public static CustomBlendMode Parse(BlendOp op, BlendMode src, BlendMode dest)
        {
            for (var index = 0; index < All.Length; index++)
            {
                var mode = All[index];
                if (mode.Src == src && mode.Dest == dest && mode.Op == op)
                    return mode;
            }
            return Undefined;
        }
    }
}